<template>
	<div class="gk-layout-container">
		<div class="gk-layout-header">
			<s-query perms="wh:pk" @click="findPage">
				<el-form-item>
					<el-input v-model.trim="filters.title" style="width:100px" placeholder="标题" />
				</el-form-item>
				<s-dict name="pkStatus" v-model="filters.status" />
				<s-date @dateChange="function (v) { filters.createBeginTime = v }" placeholder="开始时间" />
				<s-date @dateChange="function (v) { filters.createEndTime = v }" placeholder="结束时间" />
			</s-query>
			<s-reflash reflash="false" excel="true"  @exportExcel="handleExportExcelAll">
			     
			</s-reflash>
		</div>




		<div class="gk-layout-main">
			<my-table :data="pageResult.content" :loading="loading">
				<el-table-column :label="$t('action.desc')" width="50" fixed="left" header-align="center" align="center">
					<template slot-scope="scope"> {{ scope.$index + 1 }} </template>
				</el-table-column>
				<column prop="title" label="标题" />

				<column prop="status" label="状态" key="status" :formatter="selectionFormat" />
				<column prop="createTime" label="创建时间" key="createTime" :formatter="dateFormatDay" />
				<el-table-column header-align="center" align="center" width='300' :label="$t('action.operation')">
					<template slot-scope="scope">

						<kt-button label="暗盘" v-if="scope.row.status ==2" perms="wh:pk:start" @click="handleStock(scope.$index, scope.row)" />
						<kt-button label="差异确认" v-if="scope.row.status ==2" perms="wh:pk:start" @click="finshPk(scope.row)" />
						<kt-button label="打印差异"   perms="wh:pk:start" @click="handlePreview(scope.row)" />
					
						<kt-button label="统计" v-if="scope.row.status > 8" perms="wh:pk:start" @click="handleShowAll(scope.$index, scope.row)" />
					</template>
				</el-table-column>
			</my-table>
		</div>
		<!--分页栏-->
		<div class="toolbar gk-layout-footer" style="padding:10px;">

			<el-pagination layout="total, prev, pager, next, jumper" @current-change="findPage"
				:current-page="filters.pageNum" :page-size="filters.pageSize" :total="pageResult.totalSize"
				style="float:right;">
			</el-pagination>
		</div>
		<adjust-pk :coreStockDetailViewVisible="coreStockViewVisible" @coreStockClose="coreStockHidden"
			:myParam="this.coreStockDetailViewFilters" />

		<his-list :coreStockDetailViewVisible="coreStockDetailViewVisible" 	@coreStockDetailListHidden="coreStockDetailListHidden" :title="coreStockDetailViewTitle"
				@handleExportExcel="handleExportExcel"
			:filters="this.coreStockDetailViewFilters"></his-list>

	</div>
</template>

<script>

import KtButton from "@/views/Core/KtButton"
import { format } from "@/utils/datetime"
import { formatDay } from "@/utils/datetime"
import MyTable from "@/views/Core/MyTable"
import Column from "@/views/Core/Column"
import HisList from "./HisList"
import SDate from "@/components/Kj/SDate"
import SDict from "@/components/Kj/SDict"
import { myPrint, addHtmlTd, addHtmlSpan, rowSpan } from "@/utils/LodopFuncs"
 
import AdjustPk from "./AdjustPk"
import { getItemName, dictsFormat, getItem } from "@/utils/myUtil"
import SQuery from "@/components/Kj/SQuery"
import SReflash from "@/components/Kj/SReflash"
import { myXlsExport } from "@/excel/myXls"
import XLSX from "xlsx";
export default {
	components: {
		KtButton,
		MyTable,
		Column,
		HisList,
		SQuery,
		SReflash,
		SDict,SDate,
		AdjustPk
	},
	data() {
		return {
			filters: {
				label: '', pageNum: 1, pageSize: this.global.pageSize
			},
			loading: false,
			pkFormData: {},
			pageResult: {},
			operation: true,
			editLoading: false,
			coreStockDetailViewTitle: '',
			coreStockViewVisible: false,
			coreStockDetailViewFilters: {},
			dataForm: {
				id: null,
				craneId: null,
				shelfId: null,
				itemId: null

			},
			coreStockDetailViewVisible: false
		}
	},
	methods: {
		// 获取分页数据
		findPage: function (data) {
			if (data != -1) {
				data = data || 1;
				this.filters.pageNum = data
			}
			this.$api.pk.findPage(this.filters).then((res) => {
				this.pageResult = res.data
			})
		},
		coreStockDetailListHidden: function () {
			this.coreStockDetailViewVisible = false
		},
		handleShowAll: function (params, r) {
			this.coreStockDetailViewFilters.pkId = r.id;
			this.coreStockDetailViewTitle = "盘库明细";
			this.coreStockDetailViewVisible = true;

		},
		coreStockHidden: function () {
			this.coreStockViewVisible = false
		},
		handleExportExcel(row) {
			var p = {};
			p.pkId = row.pkId;
			this.excelData(p,"盘库明细"+p.pkId )
		},
		handleExportExcelAll(row) { 
			this.excelData(this.filters,"区间")
		},
		excelData(p,title){
			this.$api.pk.hisPage(p).then((res) => {
				const tHeader = ['物料', '批号', '原有数量', '现有数量', '差异', '单位'];
				const filterVal = ['item', 'batchNo', 'old', 'now', 'diff', 'unit'];
				const list = res.data;
				for (var i = 0; i < list.length; i++) {
					list[i].item = getItemName(list[i].itemId);
					list[i].diff = list[i].now - list[i].old;
					var item = getItem(list[i].itemId);

					list[i].unit = dictsFormat('units', item.u);
				}
				myXlsExport(tHeader, filterVal, list, '盘库记录' + title);
			})

		},
		handleStock: function (params, r) {
			this.coreStockDetailViewFilters.pkId = r.id;

			this.coreStockViewVisible = true;

		},
		finshPk: function (row) {

			this.$confirm("确定盘库结束", this.$t('action.tips'), {}).then(() => {
				this.editLoading = true
				let params = Object.assign({}, row)
				this.$api.pk.finshPk(params).then((res) => {
					this.editLoading = false
					if (res.code == 200) {
						this.$message({ message: this.$t('action.operateSucess'), type: 'success' })
					} else {
						this.$message({ message: this.$t('action.operateFail') + res.msg, type: 'error' })
					}
					this.findPage(this.filters.pageNum)
				})
			})
		},
		handlePreview: function (row) {

			this.$api.pk.printPkDiff({ pkId: row.id }).then((res) => {
				var strList = res.data.info;
				var html = res.data.html;
				var detailHtml = "";

				for (let index = 0; index < strList.length; index++) {
					var detail = strList[index];
					detailHtml += "<tr>";

					let rowS = rowSpan(index, strList, "boxCode");
					if (rowS > 0) {
						detailHtml += addHtmlSpan(detail.boxCode, rowS);
					}

					detailHtml += addHtmlTd(getItemName(detail.itemId));
					var statusName = dictsFormat("storeStatus", detail.status)
					detailHtml += addHtmlTd(statusName);
					detailHtml += addHtmlTd(detail.countDb);
					detailHtml += addHtmlTd(dictsFormat("units", detail.unit));
					detailHtml += "</tr>";
				}


				html = html.replace("{title}", row.title);
				html = html.replace("{Content}", detailHtml);

				myPrint("差异", html);

			})

		},




		format02null: function (row, column, cellValue, index) {
			if (cellValue == 0) {
				return "";

			}
			return cellValue;
		},

		// 时间格式化
		dateFormat: function (row, column, cellValue, index) {
			return format(row[column.property])
		},
		// 时间格式化
		dateFormatDay: function (row, column, cellValue, index) {

			return formatDay(row[column.property])
		},




		selectionFormat: function (row, column, cellValue, index) {
			let key = ""
			let propt = column.property;
			if (propt == "status") {
				key = "pkStatus"
			}
			return dictsFormat(key, cellValue);

		}
	},
	mounted() {

		this.findPage(1)
	}
}
</script>

